home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / CPP / ACGIFREE.ZIP / INCLUDE / A_CONV.H < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-15  |  10.7 KB  |  226 lines

  1. //
  2. //a_conv.h
  3. //
  4. // Conversion, Econding, Encryption, and such
  5. //
  6.  
  7. //a_Algorithm specific defines and such
  8. #include "a_random.h"             
  9.  
  10. /////////////////////////////////////////////////////////////////////
  11. // AConverto - conversion, encoding, etc.
  12. /////////////////////////////////////////////////////////////////////
  13. class AConverto : public ABase
  14. {
  15.   public:
  16.      AConverto();
  17.      virtual ~AConverto();
  18.  
  19.      //a_Variables
  20.      enum enumConvertoType
  21.      {
  22.         eatNone = 0x00000000, eat4Bit = 0x00000001, eat6Bit = 0x00000002,
  23.         eatMask = 0x0000000F,
  24.      
  25.         //a_Used for CRCs and checksums, many methods for added security
  26.         //a_There are 3 versions: BYTE, WORD and DWORD return types
  27.         ecrcXOR  = 0x00000100, ecrcModuloSum = 0x00000200, ecrcRNWCRC = 0x00000300,
  28.         ecrcMask = 0x00000F00
  29.      };
  30.  
  31.     //a_Conversion routine wrappers
  32.     int  convertoFromAlpha(UINT uType, char **pcInBlock, int iLength, int iReAllocate = 0x0);  //a_From alphanumeric to data
  33.     int  convertoToAlpha(UINT uType, char **pcInBlock, int iLength, int iReAllocate = 0x1);    //a_From data to alphanumeric
  34.     void convertoSetMap(const char *pccNewMap = NULL);                                         //a_NULL resets to the built in map
  35.  
  36.     //a_URL econding/decoding
  37.     char *convertoEncodeURL(const char *pccSource, int iSourceLength = -1);
  38.     char *convertoDecodeURL(const char *pccSource, int iZeroTest = 1);                         //a_iZeroTest will check and ignore %00 which translate into a NULL-terminator
  39.  
  40.     //aCounts # of bits in a BYTE
  41.     int convertoBitCount(BYTE bTest)
  42.     {
  43.       static BYTE bCount[0x10] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4};
  44.       return (bCount[bTest & '\x0F'] + bCount[(bTest >> 0x4) & '\x0F']);
  45.     }
  46.  
  47.     //a_HEX <-> BYTE conversions
  48.     char *convertoBYTEtoHEX(BYTE byteV, char *pcRet);    //a_Creates a 2 BYTE HEX string from a BYTE value
  49.     BYTE  convertoHEXtoBYTE(char cHi, char cLo);        //a_Creates a BYTE number from a 2 BYTE HEX string
  50.     char *convertoHEXplusplus(char *pcRet);              //a_Increment a 2 BYTE hex # [00-FF]
  51.  
  52.     //a_COLORREF conversion
  53.     char *convertoCOLORREFtoChar(COLORREF cr, char *pcRet);
  54.  
  55.     //a_Data processing, checksums, etc for data authentication and verification purposes
  56.     //a_Accepts user CRCMODEL used for ecrcRNWCRC type
  57.     BYTE  convertoCRCasBYTE(int iType, const char *pccData, int iDataLength, PCRCMODEL pcmUserModel = NULL);
  58.     WORD  convertoCRCasWORD(int iType, const char *pccData, int iDataLength, PCRCMODEL pcmUserModel = NULL);
  59.     DWORD convertoCRCasDWORD(int iType, const char *pccData, int iDataLength, PCRCMODEL pcmUserModel = NULL);
  60.  
  61.     //a_Mirrors the BYTE from LSB-MSB to MSB-LSB
  62.     BYTE convertoMirrorBYTE(BYTE bX) const
  63.     {
  64.       //a_Mirror mapping:             0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xA, 0xB, 0xC, 0xD, 0xE, 0xF
  65.       static BYTE s_bMirror[0x10] = { 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE, 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF };
  66.  
  67.       BYTE bRet  = s_bMirror[(bX & 0xF)] << 0x4;
  68.            bRet |= (s_bMirror[((bX >> 0x4) & 0xF)] & 0xF);
  69.  
  70.       return bRet;
  71.     }
  72.  
  73.     //a_Returns the value dwSource with the bottom iBits [0,32] bits reflected. Slower than convertoMirrorBYTE
  74.     //a_Example: convertMirrorLowerDWORDBits(0x3e23L, 0x3) == 0x3e26
  75.     //a_NOTE: convertoMirrorBYTE is much faster for mirroring BYTEs
  76.     DWORD convertoMirrorLowerDWORDBits(DWORD dwSource, int iBits);
  77.  
  78.   protected:
  79.     //-------------------------------------------------------------------------------------------------
  80.     //a_Conversion routines to and from AlphaNumeric
  81.     //-------------------------------------------------------------------------------------------------
  82.     //a_4Bit routines convert hex value in stream to a 2 byte hex equivalent, (eg. AB -> \x41\x42), doubles buffer size in process
  83.     int   _convertoTo4BitAlpha(char **, int, int iReAllocate = 0x1);   //a_Returns the new length of the buffer
  84.     int   _convertoFrom4BitAlpha(char **, int, int iReAllocate = 0x0);      //a_Takes pointer to NULL terminated alphanumeric string, converts to data and returns length
  85.  
  86.     //-------------------------------------------------------------------------------------------------
  87.  
  88.     //a_6Bit routines
  89.     int  _convertoTo6BitAlpha(char **, int, int iReAllocate = 0x1);                   //a_Returns the new length of the buffer
  90.     int  _convertoFrom6BitAlpha(char **, int, int iReAllocate = 0x0);                 //a_Takes pointer to NULL terminated alphanumeric string, converts to data and returns length
  91.     WORD _convertoMap6BitAlphaToWORD(BYTE bX, const char *pccMap, int iShift = 0x0)   //a_Mapping to WORD with shift from alphanumeric
  92.     {
  93.       assert(pccMap);
  94.       const char *pccX = strchr(pccMap, bX);
  95.       if (pccX)
  96.         return (WORD(pccX - pccMap) << iShift);
  97.       else
  98.         return 0;
  99.  
  100.     }
  101.     //a_6Bit mapping set (64 values = 2^6)
  102.     //a_If you override and it contains non-alphanums then the purpose of these functions is changed
  103.     //a_ it will act as a cypher-like-map and not an ASCII-map it is as a default  
  104.     static const char sm_ccMap[65];        //a_Default is defined and static, can be overridden for security purpose
  105.     char *m_pcUserMap;              //a_User can override the 64 character map (+1 for NULL)
  106.  
  107.     //-------------------------------------------------------------------------------------------------
  108.     //a_CRC Model and it's initializers
  109.     CRCMODEL m_crcModel;
  110.     void  _convertoInit8BitCRC(void);
  111.     void  _convertoInit16BitCRC(void);
  112.     void  _convertoInit32BitCRC(void);
  113.     void  _convertoDoBlockCRC(const char *pccBuffer, int iLength = -0x1);
  114.     DWORD _convertoGetCurrentCRC(void);
  115. };
  116.  
  117. //////////////////////////////////////////////////////////////////////
  118. //ARandom - Random Number Generation object
  119. //////////////////////////////////////////////////////////////////////
  120. class ARandom : public ABase
  121. {
  122.   public:
  123.     ARandom() {};
  124.     virtual ~ARandom() {};
  125.  
  126.       //a_Declares debug/dump related functions
  127.     #ifdef _DEBUG_DUMP_
  128.       void dump(void);     //a_Debugging dump, when _DEBUG_DUMP_ is set
  129.     #endif
  130.  
  131.     //a_Park and Miller "Minimal" generator U(0,1) - uniform exclusive 0.0 to 1.0
  132.     double rngMinimal(long &lSeed);
  133.  
  134.     //a_Long period L'Ecuyer with Bays-Durham shuffle (no it's not a dance!)
  135.     //a_Takes almost twice as long as rngMinimal...
  136.     double rngLEcuyer(long &lSeed);
  137.  
  138.     //a_Random # generators
  139.     //a_Inclusive # in range U[iMin, iMax], true uniform distribution adjustment
  140.     //a_Must supply a function to use as an RNG
  141.     int rngRandom(long &lSeed, int iMax, int iMin = 0x0, double (ARandom::*pfRNG)(long &) = NULL);
  142.  
  143.     //a_Routine for generating an array of random BYTEs from a given seed, user must delete[] when done
  144.     BYTE *rngGenerateRandomArray(long &lSeed, int iSize, double (ARandom::*pfRNG)(long &) = NULL);
  145.  
  146.     //a_Shuffle contents of a user provided array, (accepts a pointer to a function of tn RNG)
  147.     void rngShuffleArray(long &lSeed, BYTE *pbArray, int iSize, double (ARandom::*pfRNG)(long &) = NULL);
  148.  
  149.     //a_Chi-Square test (ported from "Algorithms in C" by R. Sedgewick; p.517)
  150.     //a_N is the number of random samples to generate and test
  151.     //a_iRange is the [0, iRange) to use in this test
  152.     float rngChiSquareTest(long &lSeed, int iN, int iRange, double (ARandom::*pfRNG)(long &) = NULL);
  153. };
  154.  
  155. //a_Generic function definition for a RNG
  156. typedef double (ARandom::*PFN_DRNG)(long &lSeed);
  157.  
  158. ////////////////////////////////////////////////////////////////
  159. // ACrypto - encryption class
  160. ////////////////////////////////////////////////////////////////
  161. class ACrypto : RTTI_VIRTUAL public AConverto, RTTI_VIRTUAL public ARandom
  162. {
  163.   public:
  164.     //a_Construction and destruction
  165.     ACrypto();
  166.     virtual ~ACrypto();
  167.     
  168.     //a_Declares debug/dump related functions
  169.     #ifdef _DEBUG_DUMP_
  170.       void dump(void);     //a_Debugging dump, when _DEBUG_DUMP_ is set
  171.     #endif
  172.  
  173.     //a_Key control
  174.     int cryptoSetKey(const BYTE *pcbNewKey, int iLength);       //a_Assign a new key
  175.     const BYTE *cryptoGetKey(int &iLength)
  176.     { iLength = m_iKeyLength; return m_pbKey; }          //a_Pointer to the key (NULL if none)
  177.     
  178.     //a_Key and string generation using random variables (lSeed == 0x0 means use time())
  179.     //a_lSeed should be negative (see ARandom for more details)
  180.     //a_Returns the seed value used so you may continue this random series
  181.     long cryptoSetRandomKey(int iSize, long lSeed = 0x0L);
  182.  
  183.     //a_Variables
  184.     enum enumCypherType
  185.     {
  186.       ectNone = 0x00000000, ectXOR_Blitter = 0x00000010, ectXOR_Convolver = 0x00000020,
  187.       ectXOR_ShiftingConvolver = 0x00000040,
  188.         ectMask = 0x000000F0
  189.      };
  190.      
  191.     //a_Stream encryptor wrappers, accepts a buffer, works on it and returns it's pointer for convenience or new length
  192.     int cryptoEncryptOnly(UINT uType, char *pcInBlock, int iLength);
  193.     char *cryptoDecryptOnly(UINT uType, char *pcInBlock, int iLength);
  194.  
  195.     //a_Main wapper functions
  196.     //a_D/Encryptor routines that convert to/from AlphaNumeric stream not BYTE data
  197.     int cryptoEncrypt(UINT uType, char **pcInBlock, int iLength = -1, int iReAllocate = 0x1);
  198.     int cryptoDecrypt(UINT uType, char **pcInBlock, int iLength, int iReAllocate = 0x0);
  199.     
  200.   protected:
  201.     //a_Key
  202.     BYTE *m_pbKey;
  203.     int   m_iKeyLength;
  204.     
  205.     //-------------------------------------------------------------------------------------------------
  206.     //a_Encryption/Decryption Algorithms
  207.     //-------------------------------------------------------------------------------------------------
  208.     //a_XOR Blitter
  209.     //a_Uses keys sequentially to just do XORs, very fast but not that secure, no shifting just a blit-like process
  210.     //a_Does both encryption and decryption due to non-shifting property
  211.     char *_StandardXORBlit(char *pcInBlock, int iLength);
  212.     //-------------------------------------------------------------------------------------------------
  213.     //a_XOR Convolver
  214.     //a_Uses all the keys and shifts(convolves) them with the input
  215.     char *_ConvolvingXOREncrypt(char *pcInBlock, int iLength);
  216.     char *_ConvolvingXORDecrypt(char *pcInBlock, int iLength);
  217.     //-------------------------------------------------------------------------------------------------
  218.     //a_XOR Shifting Convolver
  219.     //a_Uses all the keys and shifts(convolves) them with the input with a key shifted by the input
  220.     char *_ShiftingConvolvingXOREncrypt(char *pcInBlock, int iLength);
  221.     char *_ShiftingConvolvingXORDecrypt(char *pcInBlock, int iLength);
  222.     //-------------------------------------------------------------------------------------------------
  223. };
  224.  
  225. static ACrypto g_aCrypto;   //a_1 global object always exists for use by hierarchy
  226.